#include <iostream>
#include "sort.h"
#include <list>
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

using namespace std;
Sort::Sort(list<MatchedLogRec> matches,list<struct tm > loginDay,list<struct tm >logoutDay){
	this->matches = matches;
	this->loginDay = loginDay;
	this->logoutDay = logoutDay;
}

void Sort::sort_day(){
	printf("init day!");
	list<struct tm >::iterator it;
	list<struct tm>::iterator it2;
	list<MatchedLogRec>::const_iterator it1;
	char ymd[100] = {};
	for(it = loginDay.begin(),it1 = matches.begin(),it2 = logoutDay.begin();it!=loginDay.end()&&it1!=matches.end()&&it2!=logoutDay.end();++it,++it1,++it2){
	snprintf(ymd,100,"/home/wrj/day/%d-%d-%d.doc",it->tm_year,it->tm_mon,it->tm_mday);
	printf("%s\n",ymd);
	int fd = open(ymd,O_CREAT|O_RDWR|O_APPEND,0666);
	if(fd == -1){
		perror("fileError!"),exit(-1);	
	}
	char canBeSee[500] = {};
	int size = snprintf(canBeSee,500,"用户登录名：%s\n  登入时刻：%d-%d-%d %d:%d:%d\n  登出时刻：%d-%d-%d %d:%d:%d\n 在线时间：%d s\n 登入IP：%s\n",it1->log_name,it->tm_year,it->tm_mon,it->tm_mday,it->tm_hour,it->tm_min,it->tm_sec,it2->tm_year,it2->tm_mon,it2->tm_mday,it2->tm_hour,it2->tm_min,it2->tm_sec,it1->duration,it1->log_ip);
	int writeFd = write(fd,canBeSee,size);
	close(fd);
	close(writeFd);
}

}

void Sort::sort_mon(){
	printf("init mon!");
	list<struct tm >::iterator it;
	list<struct tm>::iterator it2;
	list<MatchedLogRec>::const_iterator it1;
	char ymd[100] = {};
	for(it = loginDay.begin(),it1 = matches.begin(),it2 = logoutDay.begin();it!=loginDay.end()&&it1!=matches.end()&&it2!=logoutDay.end();++it,++it1,++it2){
	snprintf(ymd,100,"/home/wrj/mon/%d/%d-%d-%d.doc",it->tm_mon,it->tm_year,it->tm_mon,it->tm_mday);
	printf("%s\n",ymd);
	int fd = open(ymd,O_CREAT|O_RDWR|O_APPEND,0666);
	if(fd == -1){
		perror("fileError!"),exit(-1);	
	}
	char canBeSee[500] = {};
	int size = snprintf(canBeSee,500,"用户登录名：%s\n  登入时刻：%d-%d-%d %d:%d:%d\n  登出时刻：%d-%d-%d %d:%d:%d\n 在线时间：%d s\n 登入IP：%s\n",it1->log_name,it->tm_year,it->tm_mon,it->tm_mday,it->tm_hour,it->tm_min,it->tm_sec,it2->tm_year,it2->tm_mon,it2->tm_mday,it2->tm_hour,it2->tm_min,it2->tm_sec,it1->duration,it1->log_ip);
	int writeFd = write(fd,canBeSee,size);
	close(fd);
	close(writeFd);
}

}

void Sort::sort_year(){
	printf("init year!");
	list<struct tm >::iterator it;
	list<struct tm>::iterator it2;
	list<MatchedLogRec>::const_iterator it1;
	char ymd[100] = {};
	for(it = loginDay.begin(),it1 = matches.begin(),it2 = logoutDay.begin();it!=loginDay.end()&&it1!=matches.end()&&it2!=logoutDay.end();++it,++it1,++it2){
	snprintf(ymd,100,"/home/wrj/year/%d/%d-%d-%d.doc",it->tm_year,it->tm_year,it->tm_mon,it->tm_mday);
	printf("%s\n",ymd);
	int fd = open(ymd,O_CREAT|O_RDWR|O_APPEND,0666);
	if(fd == -1){
		perror("fileError!"),exit(-1);	
	}
	char canBeSee[500] = {};
	int size = snprintf(canBeSee,500,"用户登录名：%s\n  登入时刻：%d-%d-%d %d:%d:%d\n  登出时刻：%d-%d-%d %d:%d:%d\n 在线时间：%d s\n 登入IP：%s\n",it1->log_name,it->tm_year,it->tm_mon,it->tm_mday,it->tm_hour,it->tm_min,it->tm_sec,it2->tm_year,it2->tm_mon,it2->tm_mday,it2->tm_hour,it2->tm_min,it2->tm_sec,it1->duration,it1->log_ip);
	int writeFd = write(fd,canBeSee,size);
	close(fd);
	close(writeFd);
}

}




